package y.q.h.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import y.q.h.mapper.TAdminMapper;
import y.q.h.entity.TAdmin;
import y.q.h.service.TAdminService;
import org.springframework.stereotype.Service;
import y.q.h.util.AESMysql;
import y.q.h.util.JavaWebToken;
import y.q.h.util.apicontroller.R;

import javax.xml.ws.soap.Addressing;
import java.util.HashMap;
import java.util.Random;

import static y.q.h.util.apicontroller.R.failed;

/**
 * (TAdmin)表服务实现类
 *
 * @author makejava
 * @since 2024-04-25 15:46:30
 */
@Service
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService {

    @Autowired
    private TAdminMapper tAdminMapper;

    @Override
    public R getAdminLogin(TAdmin tAdmin, String ipAddr) {
        //查询账户
        TAdmin tGuanliyuan1 = tAdminMapper.selectUsername(tAdmin.getAdminname());
        //判断查询的用户不为空
        if (tGuanliyuan1 == null) {
            return failed("该账户不存在");
        }
        //判断登录ip是否有限制
        if (tGuanliyuan1.getIps() != null && !"".equals(tGuanliyuan1.getIps())) {
            if (!tGuanliyuan1.getIps().contains(ipAddr)) {
                return failed("请使用正确ip登录");
            }
        }
        //查询出数据库的密码 进行解密
        String mima = tGuanliyuan1.getPassword();
        //拿解密后的数据 和 用户输入的密码进行比对
        if (tAdmin.getPassword().equals(mima)) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("zhanghu", tGuanliyuan1.getAdminname());
            map.put("dateTime",System.currentTimeMillis() / 1000);
            //通过jwt加密获取token
            String token = JavaWebToken.createJavaWebToken(map);
            map.put("token", token);
            //存储到redis中
            return R.success(map);
        } else {
            return failed("密码错误");
        }
    }
}

